package com.example.base.http;

import com.example.base.http.exception.HandleException;
import com.example.base.http.exception.ApiException;
import com.example.base.utils.ToastUtils;

import io.reactivex.observers.DisposableObserver;

/**
 * 请求统一封装处理
 */

public abstract class ApiObserver<T> extends DisposableObserver<T> {
    public abstract void onResult(T t);

    @Override
    public void onComplete() {

    }

    @Override
    public void onError(Throwable e) {
        e.printStackTrace();
        if (e instanceof ApiException) {
            ApiException rError = HandleException.handleException(e);
            ToastUtils.toast(rError.message);
            return;
        }
        //其他全部甩锅网络异常
        ToastUtils.toast("网络异常");
    }

    @Override
    public void onNext(T response) {
        ResponseData responseData = (ResponseData) response;
        if (responseData.isSuccess()) {
            onResult((T) responseData);
        } else {
            ToastUtils.toast(responseData.getMessage());
        }
    }
}